package com.fth.learn.chapter03;

/**
 * 学习一下二分
 * YiMi
 *
 * @author YiMi
 * @version 1.0
 * @date 2023/5/13 14:51
 */

public class BinarySearch {
    /**
     * 假如说有一组有序的数
     * @param start 开始
     * @param end 末尾
     * @param target 目标
     */

    public  static  boolean binarySearchReal(int start,int end,int target){
        checkException(start,end);
        return BinarySearch(start,end,target);
    }

    //容错处理
    public  static  void checkException(int start,int end){
        if (start > end){
            throw  new RuntimeException("参数有问题");
        }
    }

    //计算功能
    public  static  boolean BinarySearch(int start,int end,int target){

        if(start == end){
            return start == target;
        }

        while (start <= end){
            int middle = (end + start) >>1;
            if(middle == target){
                return true;
            }else if(middle < target){
                start = middle + 1;
                //middele 已经算完了 middele的右面第一位有可能等于
            }else {
                end = middle - 1;
            }

        }
        return false;
    }



    public static void main(String[] args) {
        System.out.println(binarySearchReal(1,10,7));
    }
}
